﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Sgr.Data;
using Sgr.EntityFrameworkCore.EntityConfigurations;
using Sgr.UPMS.Domain.Duties;

namespace Sgr.UPMS.Infrastructure.EntityConfigurations
{
    internal class DutyEntityTypeConfiguration : EntityTypeConfigurationBase<Duty, long>
    {
        public DutyEntityTypeConfiguration(DatabaseType? databaseType = null) : base(databaseType)
        { }

        public override void Configure(EntityTypeBuilder<Duty> builder)
        {
            builder.ToTable("upms_duty");

            base.Configure(builder);

            builder.PropertyAndHasColumnName(b => b.Code, GetColumnNameCase())
                .HasMaxLength(200)
                .IsRequired()
                .HasComment("职务编码");

            builder.PropertyAndHasColumnName(b => b.Name, GetColumnNameCase())
                .HasMaxLength(200)
                .IsRequired()
                .HasComment("职务名称");

            builder.PropertyAndHasColumnName(b => b.OrderNumber, GetColumnNameCase())
                .IsRequired()
                .HasComment("排序号");

            builder.PropertyAndHasColumnName(b => b.State, GetColumnNameCase())
                .IsRequired()
                .HasComment("职务状态");

            builder.PropertyAndHasColumnName(b => b.Remarks, GetColumnNameCase())
                .HasMaxLength(500)
                .HasComment("职务备注");

            //builder.Property<long?>("TenantId")
            //    .HasColumnName(EntityTypeBuilderExtension.GetColumnName("TenantId", "m", GetColumnNameCase()))
            //    .IsRequired(false)
            //    .HasComment("租户ID（备用字段，为多租户功能预留）");

            //索引
            base.AddOrgIndex(builder);

            // 针对职务ID和名称的联合索引（优化查询性能）
            builder.HasIndex(b => new { b.Id, b.Name })
                .HasDatabaseName("IX_Duty_Id_Name")
                .IsUnique(false);  // 不需要唯一约束，因为是为了优化查询
        }
    }
}